package occ.service
{
import flash.data.*;
import flash.events.*;
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;

import occ.model.*;

import mx.collections.ArrayCollection;
import mx.utils.*;

import occ.utils.*;

  import flash.events.EventDispatcher;
  import flash.events.IEventDispatcher;
  
  public class Org extends EventDispatcher
  {
  
    public static const IS_INIT:String="OrgIsInitialzed";
    public static const UNDEFINED:String="OrgIsNotDefined";
    public static const SAVED:String="OnOrgSaved";
    public static const SAVE_FAILED:String="OnOrgSavedAttemptFailed";
    
    [Bindable]
    public static var defaultState:String="PA";
    [Bindable]
    public static var taxRate:Number;
    [Bindable]
    public static var name:String;
    [Bindable]
    public static var descr:String;
    
    private var sqlData:Array;
		private var stmt:SQLStatement;
		private var sql:String;
    private var o:Object;
    private var _taxRate:Number;
    
    public function Org(target:IEventDispatcher=null)
    {
      super(target);
    }
    
    public function init():void
    {
      //trace("getSiteTypes");
      sql="select * from org";
      stmt = new SQLStatement();
  		stmt.sqlConnection = DatabaseManager.getConnection();
  		stmt.addEventListener( SQLErrorEvent.ERROR, onSQLError );
  		stmt.addEventListener( SQLEvent.RESULT, onInit );
  		stmt.text = sql;
  		stmt.execute();
    }
    
    private function onInit(event:SQLEvent):void
    {
      trace("Org onInit");
      var result:SQLResult = stmt.getResult();
      try{
        sqlData = result.data;
        for(var i:int=0;i<sqlData.length;i++){
          trace("Did we enter");
          o = (sqlData[i] as Object);
    			taxRate=o.tax;
          trace("taxRate: " + taxRate);
          name = o.name;
          descr= o.descr;
          defaultState = o.default_state;
    			}
          dispatchEvent(new Event(IS_INIT));
        
      }catch(e:Error){
        dispatchEvent(new Event(UNDEFINED));
      }
    }
    
    private function onSQLError( event:SQLErrorEvent ):void
		{
      trace("Season onSQLError");
			 var errorString:String = "SQL Error: " + event.error.errorID + "nDetails: " + event.error.details + "nDetail Arguments: " +event.error.detailArguments;
 			throw new Error(errorString);
		}
    
  }
}